import React from 'react';
import {DeviceEventEmitter, NativeModules, ScrollView, StyleSheet, View,} from 'react-native';
import RippleButton from "../widget/btn/RippleButton";
import * as Color from "../constant/Color";

// 跟原生交互
export default class PageCommunicate extends React.Component {
    render() {
        return (
            <ScrollView>
                <View  style={styles.container}>
                    <View style={{height: 15}}/>
                    <RippleButton styleText={styles.text} title={'传参调用原生Toast方法'} onPress={()=>this._onPressRippleButton()}/>
                    <View style={{height: 15}}/>
                    <RippleButton styleText={styles.text} title={'传参调用原生方法并获取返回值'} onPress={()=>this._getTime()}/>
                    <View style={{height: 15}}/>
                    <RippleButton styleText={styles.text} title={'传参数与Function调用原生方法'} onPress={()=>this._getCallBackTime()}/>
                    <View style={{height: 15}}/>
                    <RippleButton styleText={styles.text} title={'通过Promise传递数据'} onPress={()=>this._getPromiseTime()}/>
                    <View style={{height: 15}}/>
                    <RippleButton styleText={styles.text} title={'调起指定Activity(带参)'} onPress={()=>this._startActivity()}/>
                </View>
            </ScrollView>
        );
    }

    componentWillMount() {
        DeviceEventEmitter.addListener('EventName', function(msg) {
            if(!NativeModules.NativeMethods) return;
            NativeModules.NativeMethods.show("收到Native消息:" + "\n" + msg.key, NativeModules.NativeMethods.SHORT);

        });
    }

    /** 传参调用原生Activity */
    _startActivity() {
        if(!NativeModules.NativeMethods) return;
        let param = new Map();

        let param1 = {
            'key1': '如果你看到这条消息,代表你成功通过JS带参调起Activity',
            'key2': 'value2',
        };

        NativeModules.NativeMethods.startActivityByClassname('view.ViewActivity', param1)
    }

    /** 传参调用原生Toast方法 */
    _onPressRippleButton() {
        if(!NativeModules.NativeMethods) return;
        NativeModules.NativeMethods.show('FUCK, DAMN IT!', NativeModules.NativeMethods.SHORT);
    }

    /** 传参调用原生方法并获取返回值 */
    _getTime() {
        if(!NativeModules.NativeMethods) return;
        NativeModules.NativeMethods.getTime();
    }

    /** 传参数与Function调用原生方法 */
    _getCallBackTime() {
        NativeModules.NativeMethods.callBackTime("Allure",
            (msg) => {
                console.log(msg);
                NativeModules.NativeMethods.show("Promise:" + msg, NativeModules.NativeMethods.SHORT);
            }
        );
    }

    /** 通过Promise传递数据 */
    _getPromiseTime() {
        NativeModules.NativeMethods
            .sendPromiseTime("Allure")
            .then(msg => {
                NativeModules.NativeMethods.show("Promise收到消息:" + "\n" + "年龄:" + msg.age + "时间:" + msg.time, NativeModules.ToastAndroid.SHORT)
            })
            .catch(error=> {
                    console.log(error);
                }
            );
    }
}

const styles = StyleSheet.create({
    container: {
        // flex: 1,
        // justifyContent: 'center',
        alignItems: 'center',
        // height: 600,
    },
    text:{
        color:Color.darkGray,
        padding:5,
    },
});

